package gbench.sandbox.jdbc;

import org.junit.jupiter.api.Test;

import static gbench.common.matlib.MatlibCanvas.println;
import static gbench.common.matlib.algebra.lisp.IRecord.REC;
import static gbench.common.matlib.algebra.lisp.Tuple2.TUP2;
import static gbench.common.matlib.jdbc.Format.FT;
import static gbench.common.matlib.jdbc.SqlBuilder.*;
import static gbench.common.matlib.algebra.num4j.PmCm.A;
import static gbench.common.matlib.jdbc.index.Algorithm.KEY_VALUE;

import gbench.common.matlib.algebra.lisp.DFrame;
import gbench.common.matlib.algebra.lisp.IRecord;
import gbench.common.matlib.jdbc.JdbcApp;
import gbench.common.matlib.jdbc.index.GlobalIndex;
import gbench.common.matlib.jdbc.index.TableSearchEngine;

/**
 * 
 * @author Administrator
 *
 */
public class JunitJdbc {

    @Test
    public void foo() {

        final var db = "erp";
        final var url = JdbcApp.mysql_url(db);
        @SuppressWarnings("unchecked")
        final var srchEngine = new TableSearchEngine(new GlobalIndex<>()) // 表数据引擎
                .initialize(REC("url", url, "algorithms", A( // 算法描述符的定义
                        TUP2(KEY_VALUE, "id, name : t_vertex"), // t_vertex 的索引
                        TUP2(KEY_VALUE, "id, name : t_coa"), // t_coa 的索引
                        TUP2(KEY_VALUE, "id, name : t_product") // t_product 的索引
                ))); // initialize
        final var decoder = srchEngine.getDecoder2("v0,v1:t_vertex");
        final var jdbc = new JdbcApp(db);

        final var dfm = jdbc.sqlquery(
                j -> j.sql2recordS(select2("t_edge", "id,v0,v1").limit(10)).map(decoder).collect(DFrame.dfmclc));
        println(dfm);

        println("--------------------------------");
        // acct 字段检索t_coa,pct,检索t_product
        final var decoder2 = srchEngine.getDecoder2(e -> e, " acct_id,acct:t_coa ; pct_id, pct : t_product ");
        final var p = decoder2.apply(REC("acct", 1, "pct", 1));
        p.forEach((level, node) -> {
            println(FT("$0$1($2)", " | ".repeat(level), node._1, node._2));
        });
        println(p.pathstr("acct/code"), p.pathstr("pct/path"));
        p.pathllS("acct/code").forEach(e -> { // 转换成流结构
            println("stream", e);
        });
    }

    @Test
    public void bar() {
        final var db = "db";
        final var rb = IRecord.rb("id,name,password,mobile,title,description");
        final var user = rb.get("int primary key auto_increment", "zhangsan", "123456", "18601690610", "销售经理", "张三");
        final var jdbc = new JdbcApp(db); // jdbc

        jdbc.withTransaction(sess -> {
            sess.sql2execute("drop table if exists t_user"); // 删除表
            sess.sql2execute(createTable("t_user", user)); // 创建表
            final var id = sess.sql2execute2int(insert("t_user", user.filterNot("id")));
            println("GENERATED_KEY", id); // 插入数据
            println(sess.sql2x("select * from t_user")); // 查询结果
        }); // withTransaction
    }

}
